Index | Diary 2024-06-12

unordered_set<int> s

definition

c++ 标准库总的 container (容器)

可以存储唯一的元素,不重复 即 hash table 查找 插入 删除 的平均时间复杂度是常数时间 \(O(1)\)

声明

unordered_set<char> occ = {'a', 'b', 'c'}

Methods

.find( )

s.find(x) 查找元素 x

.insert( )

添加元素 occ.insert('d')

.count( )

检查集合中是否存在某个元素,返回 1/0

.erase()

删除元素

occ.erase('a') occ.erase(occ.find('a'))

遍历集合

用迭代器遍历 unordered_set 中的元素

for (auto it = occ.begin(); it != occ.end(); it++) {
    cout << *it << " ";
}